home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr47 / sb16snd.zip / SBRECORD.C < prev    next >
C/C++ Source or Header  |  1995-02-18  |  4KB  |  163 lines

  1. /*           Copyright 1995 by Ethan Brodsky.  All rights reserved          */
  2.  
  3. /* sbrecord.c */
  4.  
  5. #include <conio.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9.  
  10. #include "sbio.h"
  11. #include "xms.h"
  12.  
  13. #define BASEIO 0x220
  14. #define IRQ    5
  15. #define DMA16  5
  16.  
  17. #define SAVE_CHUNK_SIZE 8192
  18. #define BLOCK_LENGTH    256
  19.  
  20. #define FALSE 0
  21. #define TRUE  1
  22.  
  23. int (far *bufptr)[2][BLOCK_LENGTH] = NULL;
  24.  
  25. float time;
  26. unsigned int rate;
  27. char  filename[64] = "";
  28.  
  29. long  numsamples;
  30.  
  31. int   handle;
  32. long  curoffset;
  33. long  datasize;
  34.  
  35. int getparameters(int argc, char *argv[], float *time, unsigned int *rate, char *fname)
  36.   {
  37.     if (argc != 4) /* The program name counts as an argument */
  38.       return FALSE;
  39.       else
  40.         {
  41.           if ((*time = atof(argv[1])) == 0.0) return FALSE;
  42.           if ((*rate = atoi(argv[2])) == 0)   return FALSE;
  43.           strcpy(fname, argv[3]);
  44.           return TRUE;
  45.         }
  46.   }
  47.  
  48. MOVEPARAMS save_moveparams;
  49. void savedata(void)
  50.   {
  51.     FILE *f;
  52.     char chunk[SAVE_CHUNK_SIZE];
  53.  
  54.     if ((f = fopen(filename, "wb")) == NULL)
  55.       {
  56.         printf("Error opening %s!\n", *filename);
  57.         exit(EXIT_FAILURE);
  58.       }
  59.  
  60.     save_moveparams.sourcehandle = handle;
  61.     save_moveparams.sourceoffset = 0;
  62.     save_moveparams.desthandle   = 0;
  63.     save_moveparams.destoffset   = (long)(&chunk);
  64.  
  65.     memset(&chunk, 0x00, SAVE_CHUNK_SIZE);
  66.  
  67.     while (datasize)
  68.       {
  69.         save_moveparams.length = /* min(SAVE_CHUNK_SIZE, datasize) */
  70.           (datasize > SAVE_CHUNK_SIZE) ? SAVE_CHUNK_SIZE : datasize;
  71.         xms_move(&save_moveparams);
  72.         fwrite(&chunk, 1, (size_t)save_moveparams.length, f);
  73.         save_moveparams.sourceoffset += save_moveparams.length;
  74.         datasize -= save_moveparams.length;
  75.       }
  76.  
  77.     fclose(f);
  78.   }
  79.  
  80.  
  81. MOVEPARAMS record_moveparams;
  82. void far recordhandler(void)
  83.   {
  84.     if (curoffset < datasize)
  85.       {
  86.         record_moveparams.length =
  87.           ((curoffset+BLOCK_LENGTH*2) <= datasize) ? (BLOCK_LENGTH*2) : (datasize - curoffset);
  88.         record_moveparams.sourcehandle = 0;
  89.         record_moveparams.sourceoffset = (long)((*bufptr)[curblock]);
  90.         record_moveparams.desthandle   = handle;
  91.         record_moveparams.destoffset   = curoffset;
  92.  
  93.         xms_move(&record_moveparams);
  94.         curoffset += BLOCK_LENGTH*2;
  95.       }
  96.   }
  97.  
  98. void init(void)
  99.   {
  100.     getbuffer(&(int far *)bufptr, BLOCK_LENGTH);
  101.     numsamples = time*rate;
  102.  
  103.     xms_init();
  104.     datasize = numsamples * 2;
  105.  
  106.     if (xms_allocate(&handle, (unsigned int)((datasize / 1024) + 1)))
  107.       {
  108.         curoffset = 0;
  109.  
  110.         sethandler(recordhandler);
  111.         init_sb(BASEIO, IRQ, DMA16, input, rate);
  112.         startio(numsamples);
  113.       }
  114.     else
  115.       {
  116.         printf("ERROR:  Not enough free XMS!\n");
  117.         printf("        Bytes required:  %u\n", 2 * numsamples);
  118.         printf("        Bytes free:      %u\n", 1024 * xms_getfreemem());
  119.         exit(EXIT_FAILURE);
  120.       }
  121.   }
  122.  
  123. void shutdown(void)
  124.   {
  125.     shutdown_sb();
  126.     sethandler(NULL);
  127.     freebuffer(&(int far *)bufptr);
  128.  
  129.     xms_free(&handle);
  130.   }
  131.  
  132. int main(int argc, char *argv[])
  133.   {
  134.     printf("SBRECORD - Copyright 1995 by Ethan Brodsky.  All rights reserved.\n");
  135.  
  136.     if (getparameters(argc, argv, &time, &rate, filename))
  137.       printf("Recording for %.2f seconds at %u HZ to %s\n", time, rate, filename);
  138.     else
  139.        {
  140.          printf("Syntax:  sbrecord <time> <rate> <filename>\n");
  141.          printf("Example: sbrecord 2.0 22050 test.raw\n");
  142.          exit(EXIT_FAILURE);
  143.        }
  144.  
  145.     init();
  146.  
  147.     while (!(done || kbhit()));
  148.  
  149.     if (done)
  150.       savedata();
  151.  
  152.     if (kbhit())
  153.       {
  154.         printf("Recording canceled by keypress\n");
  155.         getch();
  156.       }
  157.  
  158.     shutdown();
  159.  
  160.     printf("\n");
  161.  
  162.     return(EXIT_SUCCESS);
  163.   }